package hut.ai.team.object;

import hut.ai.team.inf.IClickable;
import hut.ai.team.resource.ImageData;
import hut.ai.team.screen.MainScreen;
import hut.ai.team.table.VanCo;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Group;

public class QuanCo extends Group implements IClickable{
	public int Hang;
	public int Cot;
	public int Loai; // Loai quan co {Tuong = 1,Sy = 2,Tinh = 3,Xe = 4,Phao = 5,Ma = 6,Chot = 7}
	public int LoaiCL; // Loai quan co {Tuong = 1,Sy = 2,Tinh = 3,Xe = 4,Phao = 5,Ma = 6,Chot = 7}
	public int Phe;// Phe = 0 (Tai o do ko co quan co) || Phe = 1 || Phe = 2
	public int TrangThai;// TrangThai = 0: QuanCo da bi an | Trangthai = 1: QuanCo van con song chua lat | TrangThai = 2: QuanCo da duoc lat len
	public boolean Khoa = false;
	public boolean isClick = false;
	public boolean isVisible = true;
	public boolean biChieu;
	
	public TextureRegion mImageFlip;
	public TextureRegion mImageFlipSelect;
	public TextureRegion mImage;
	public TextureRegion mImageSelect;
	public TextureRegion mImageChieu;
	
	public QuanCo() {
		this.Hang = 10;
		this.Cot = 10;
		this.Loai  = 0;
		this.LoaiCL = 0;
		this.Phe = 0;
		this.TrangThai = 0;
		this.Khoa = true;
		this.isClick = false;
		this.isVisible = true;
		this.biChieu = false;
	}
	
	public void setTextureRegion(){
		
		this.mImageFlip = ImageData.getTextureRegion("hidden");
		this.mImageFlipSelect = ImageData.getTextureRegion("hidden_marked");
		if(this.Phe == 1){
			this.mImageChieu = ImageData.getTextureRegion("rtuong_bichieu");
			switch (this.Loai) {
			case 1:
				this.mImage = ImageData.getTextureRegion("rtuong");
				this.mImageSelect = ImageData.getTextureRegion("rtuong_marked");
				break;
			case 2:
				this.mImage = ImageData.getTextureRegion("rsy");
				this.mImageSelect = ImageData.getTextureRegion("rsy_marked");
				break;
			case 3:
				this.mImage = ImageData.getTextureRegion("rtinh");
				this.mImageSelect = ImageData.getTextureRegion("rtinh_marked");
				break;
			case 4:
				this.mImage = ImageData.getTextureRegion("rxe");
				this.mImageSelect = ImageData.getTextureRegion("rxe_marked");
				break;
			case 5:
				this.mImage = ImageData.getTextureRegion("rphao");
				this.mImageSelect = ImageData.getTextureRegion("rphao_marked");
				break;
			case 6:
				this.mImage = ImageData.getTextureRegion("rma");
				this.mImageSelect = ImageData.getTextureRegion("rma_marked");
				break;
			case 7:
				this.mImage = ImageData.getTextureRegion("rchot");
				this.mImageSelect = ImageData.getTextureRegion("rchot_marked");
				break;
			default:
				this.mImage = ImageData.getTextureRegion("rtuong");
				this.mImageSelect = ImageData.getTextureRegion("rtuong_marked");
				break;
			}
		}else{
			this.mImageChieu = ImageData.getTextureRegion("btuong_bichieu");
			switch (this.Loai) {
			case 1:
				this.mImage = ImageData.getTextureRegion("btuong");
				this.mImageSelect = ImageData.getTextureRegion("btuong_marked");
				break;
			case 2:
				this.mImage = ImageData.getTextureRegion("bsy");
				this.mImageSelect = ImageData.getTextureRegion("bsy_marked");
				break;
			case 3:
				this.mImage = ImageData.getTextureRegion("btinh");
				this.mImageSelect = ImageData.getTextureRegion("btinh_marked");
				break;
			case 4:
				this.mImage = ImageData.getTextureRegion("bxe");
				this.mImageSelect = ImageData.getTextureRegion("bxe_marked");
				break;
			case 5:
				this.mImage = ImageData.getTextureRegion("bphao");
				this.mImageSelect = ImageData.getTextureRegion("bphao_marked");
				break;
			case 6:
				this.mImage = ImageData.getTextureRegion("bma");
				this.mImageSelect = ImageData.getTextureRegion("bma_marked");
				break;
			case 7:
				this.mImage = ImageData.getTextureRegion("bchot");
				this.mImageSelect = ImageData.getTextureRegion("bchot_marked");
				break;
			default:
				this.mImage = ImageData.getTextureRegion("btuong");
				this.mImageSelect = ImageData.getTextureRegion("btuong_marked");
				break;
			}
		}
		this.setSize(mImage.getRegionWidth(), mImage.getRegionHeight());
	}
	
	public void KhoiTao(int x, int y,int loai, int stt, int phe, boolean khoa) // Khoi tao quan co voi Phe=phe,Ten=name, TrangThai=stt, Khoa=khoa, Vi Tri=[x,y])
	{
		this.Hang = x;
		this.Cot = y;
		this.Loai = loai;
		this.LoaiCL = loai;
		this.TrangThai = stt;
		this.Phe = phe;
		this.Khoa = khoa;
		this.setPosition(Cot * 50 + 18, Hang * 53 + 105);
		setTextureRegion();
	}
	
	public void setDataOnTable(){
		MainScreen.getInstance().mBanCo.ViTri[this.Hang][this.Cot].Phe = this.Phe;
		if(this.TrangThai == 1)
			MainScreen.getInstance().mBanCo.ViTri[this.Hang][this.Cot].Loai = this.LoaiCL;
		else
			MainScreen.getInstance().mBanCo.ViTri[this.Hang][this.Cot].Loai = this.Loai;
		MainScreen.getInstance().mBanCo.ViTri[this.Hang][this.Cot].TrangThai = this.TrangThai;
		MainScreen.getInstance().mBanCo.ViTri[this.Hang][this.Cot].Trong = false;
	}
	
	@Override
	public void act(float delta) {
		super.act(delta);
	}
	
	@Override
	public void draw(SpriteBatch batch, float parentAlpha) {
		super.draw(batch, parentAlpha);
		if(isVisible){
			Color color = getColor();
			Color old = batch.getColor();
			batch.setColor(color.r, color.g, color.b, color.a * parentAlpha);
			float x = getX();
			float y = getY();
			float scaleX = getScaleX();
			float scaleY = getScaleY();
			float rotation = getRotation();
			if (scaleX == 1 && scaleY == 1 && rotation == 0) {
				if(this.biChieu){
					if(this.Loai == 1){
						batch.draw(mImageChieu, x, y, getWidth(), getHeight());
					}
				}else{
					if(this.isClick){
						if(this.TrangThai == 1){
							batch.draw(mImageFlipSelect, x, y, getWidth(), getHeight());
						}else if(this.TrangThai == 2){
							batch.draw(mImageSelect, x, y, getWidth(), getHeight());
						}
					}else{
						if(this.TrangThai == 1){
							batch.draw(mImageFlip, x, y, getWidth(), getHeight());
						}else if(this.TrangThai == 2){
							batch.draw(mImage, x, y, getWidth(), getHeight());
						}
					}
				}
			} else {
				if(this.biChieu){
					if(this.Loai == 1){
						batch.draw(mImageChieu, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, rotation);
					}
				}else{
					if(this.isClick){
						if(this.TrangThai == 1){
							batch.draw(mImageFlipSelect, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, rotation);
						}else if(this.TrangThai == 2){
							batch.draw(mImageSelect, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, rotation);
						}
					}else{
						if(this.TrangThai == 1){
							batch.draw(mImageFlip, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, rotation);
						}else if(this.TrangThai == 2){
							batch.draw(mImage, x, y, getOriginX(), getOriginY(), getWidth(), getHeight(), scaleX, scaleY, rotation);
						}
					}
				}
			}
			batch.setColor(old);
		}
	}

	@Override
	public void onTouchDown() {
	}

	@Override
	public void onTouchUp() {
		if(MainScreen.getInstance().mode == 2){
			MainScreen.getInstance().n1.ResetClick();
			MainScreen.getInstance().n2.ResetClick();
		}else{
			MainScreen.getInstance().n1.ResetClick();
//			MainScreen.getInstance().computer.ResetClick();
		}
		if(!this.Khoa){
			VanCo.DanhDau = this;
			if(this.isClick){
				MainScreen.getInstance().mBanCo.ResetCanMove();
				this.isClick =  false;
			}else{
				this.isClick = true;
				onClick();
			}
		}
	}
	
	public void onClick(){
		System.out.println("Click quan co: " + this.Hang + "," + this.Cot);
		if(this.TrangThai == 1){
			click(this.LoaiCL);
		}else{
			click(this.Loai);
		}
		System.out.println();
		System.out.println("Khi click quan co");
		MainScreen.getInstance().mBanCo.showData();
	}
	
	private void click(int loai)
	{
		switch (loai) {
		case 1:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (Tuong.KiemTra(this,i, j) == 1 && Tuong.TuongAnToan(this, i, j) == 1) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		case 2:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (Sy.KiemTra(this,i, j) == 1 && Sy.TuongAnToan(this, i, j) == 1) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		case 3:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (Tinh.KiemTra(this,i, j) == 1 && Tinh.TuongAnToan(this, i, j) == 1) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		case 4:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if ((Xe.KiemTra(this,i, j) == 1) && (Xe.TuongAnToan(this, i, j) == 1) ) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		case 5:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (Phao.KiemTra(this,i, j) == 1 && Phao.TuongAnToan(this, i, j) == 1) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		case 6:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (Ma.KiemTra(this,i, j) == 1 && Ma.TuongAnToan(this, i, j) == 1) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		case 7:
			MainScreen.getInstance().mBanCo.ResetCanMove();
			for (int i = 0; i <= 9; i++) {
				for (int j = 0; j <= 8; j++) {
					if (Chot.KiemTra(this,i, j) == 1 && Chot.TuongAnToan(this, i, j) == 1) {
						MainScreen.getInstance().mBanCo.ViTri[i][j].imgMove.setVisible(true);
					}
				}
			}
			break;
		default:
			break;
		}
	}
	
	public int getDiem(){
		int diem = 0;
		switch (this.Loai) {
		case 1:
			diem = Tuong.getDiemTuong(this);
			break;
		case 2:
			diem = Sy.getDiemSy(this);
			break;
		case 3:
			diem = Tinh.getDiemTinh(this);
			break;
		case 4:
			diem = Xe.getDiemXe(this);
			break;
		case 5:
			diem = Phao.getDiemPhao(this);
			break;
		case 6:
			diem = Ma.getDiemMa(this);
			break;
		case 7:
			diem = Chot.getDiemChot(this);
			break;
		default:
			break;
		}
		return diem;
	}
}
